home *** CD-ROM | disk | FTP | other *** search
- program PQTest;
-
- {$IFDEF Win32}
- {$APPTYPE CONSOLE}
- {$ENDIF}
-
- uses
- SysUtils,
- Classes,
- {$IFDEF Windows}
- WinCrt,
- {$ENDIF}
- PriQueue in 'PriQueue.pas',
- HeapSort in 'HeapSort.pas';
-
- type
- TMyItem = class
- public
- miString : string[31];
- miPriority : integer;
- end;
-
- function CreateRandomItem : TMyItem;
- var
- i : integer;
- begin
- Result := TMyItem.Create;
- with Result do begin
- miString[0] := char(Random(10) + 20);
- for i := 1 to length(miString) do
- miString[i] := char(Random(26) + ord('A'));
- miPriority := Random(200);
- end;
- end;
-
- function MyCompare(const aItem1, aItem2 : pointer) : integer; far;
- var
- One : TMyItem absolute aItem1;
- Two : TMyItem absolute aItem2;
- begin
- if (One.miPriority < Two.miPriority) then
- Result := -1
- else if (One.miPriority = Two.miPriority) then
- Result := 0
- else
- Result := 1
- end;
-
- function MyLessThan(const aItem1, aItem2 : pointer) : boolean; far;
- var
- One : TMyItem absolute aItem1;
- Two : TMyItem absolute aItem2;
- begin
- Result := (One.miPriority < Two.miPriority);
- end;
-
- var
- i : integer;
- MyPQA : TaaPriorityQueueA;
- MyPQB : TaaPriorityQueueB;
- MyPQ : TaaPriorityQueue;
- MyPQEx : TaaPriorityQueueEx;
- Item : TMyItem;
- MyList: TList;
- MyHandles : array [1..20] of TaaPQHandle;
-
-
- begin
- Randomize;
- writeln('---Testing priority queue A---');
- MyPQA := TaaPriorityQueueA.Create(MyCompare);
- writeln(' add 20 items');
- for i := 1 to 20 do
- MyPQA.Add(CreateRandomItem);
- writeln(' remove all items');
- while (MyPQA.Count > 0) do begin
- Item := TMyItem(MyPQA.Remove);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyPQA.Free;
- readln;
-
-
- writeln('---Testing priority queue B---');
- MyPQB := TaaPriorityQueueB.Create(MyCompare);
- writeln(' add 20 items');
- for i := 1 to 20 do
- MyPQB.Add(CreateRandomItem);
- writeln(' remove all items');
- while (MyPQB.Count > 0) do begin
- Item := TMyItem(MyPQB.Remove);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyPQB.Free;
- readln;
-
-
- writeln('---Testing final priority queue---');
- MyPQ := TaaPriorityQueue.Create(MyCompare);
- writeln(' add 20 items');
- for i := 1 to 20 do
- MyPQ.Add(CreateRandomItem);
- {
- for i := 1 to 20 do begin
- Item := TMyItem(MyPQ.List[i-1]);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- readln;
- }
- writeln(' remove all items');
- while (MyPQ.Count > 0) do begin
- Item := TMyItem(MyPQ.Remove);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyPQ.Free;
- readln;
-
-
- writeln('---Testing final priority queue by passing a preset list---');
- MyList := TList.Create;
- for i := 1 to 20 do
- MyList.Add(CreateRandomItem);
- MyPQ := TaaPriorityQueue.CreateWithList(MyCompare, MyList);
- for i := 1 to 20 do begin
- Item := TMyItem(MyPQ.List[i-1]);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- readln;
- writeln(' remove all items');
- while (MyPQ.Count > 0) do begin
- Item := TMyItem(MyPQ.Remove);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyPQ.Free;
- MyList.Free;
- readln;
-
-
- writeln('---Testing heap sort---');
- MyList := TList.Create;
- for i := 1 to 20 do
- MyList.Add(CreateRandomItem);
- AAHeapSort(MyList, 2, 17, MyLessThan);
- writeln(' 3rd to 18th items should be sorted');
- for i := 1 to 20 do begin
- Item := TMyItem(MyList[i-1]);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyList.Free;
- readln;
-
-
- writeln('---Testing extended priority queue---');
- MyPQEx := TaaPriorityQueueEx.Create(MyCompare);
- writeln(' add 20 items');
- for i := 1 to 20 do
- MyPQEx.Add(CreateRandomItem);
- {
- for i := 1 to 20 do begin
- Item := TMyItem(MyPQ.List[i-1]);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- }
- writeln(' remove all items');
- while (MyPQEx.Count > 0) do begin
- Item := TMyItem(MyPQEx.Remove);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyPQEx.Free;
- readln;
-
-
-
- writeln('---Testing extended priority queue delete and replace---');
- MyPQEx := TaaPriorityQueueEx.Create(MyCompare);
- writeln(' add 20 items');
- for i := 1 to 20 do
- MyHandles[i] := MyPQEx.Add(CreateRandomItem);
- writeln(' delete every 4th item');
- MyPQEx.Delete(MyHandles[4]);
- MyPQEx.Delete(MyHandles[8]);
- MyPQEx.Delete(MyHandles[12]);
- MyPQEx.Delete(MyHandles[16]);
- MyPQEx.Delete(MyHandles[20]);
- writeln(' replace every 5th item (note the leaks)');
- MyPQEx.Replace(MyHandles[5], CreateRandomItem);
- MyPQEx.Replace(MyHandles[10], CreateRandomItem);
- MyPQEx.Replace(MyHandles[15], CreateRandomItem);
- writeln(' remove all items');
- while (MyPQEx.Count > 0) do begin
- Item := TMyItem(MyPQEx.Remove);
- writeln(' ', Item.miPriority:2, ' ', Item.miString);
- end;
- MyPQEx.Free;
- readln;
-
- end.
-